//RSA 非对称加密
import { JSEncrypt } from "jsencrypt"
import { useJsencryptStore } from "@/store/useJsencryptStore"

/**
 *  对传入的数据进行加密
 * @param value {{string}} 要加密的数据
 * @returns {{string}}  加密后的数据
 */
const encryption = (value: string): string => {
  const { publicKey } = useJsencryptStore()
  const encrypt: any = new JSEncrypt()
  encrypt.setPublicKey(publicKey)
  const encrypted = encrypt.encrypt(value)
  return encrypted
}

/**
 *  对加密后的数据进行解密 {一般不在前端解密，都是后端进行解密}
 * @param value {{string}} 要解密的数据
 * @returns {{string}}  解密后的数据
 */
const decryption = (value: string): string => {
  const decrypt = new JSEncrypt()
  const { privateKey } = useJsencryptStore()
  decrypt.setPrivateKey(privateKey)
  const decryptMsg: any = decrypt.decrypt(value)
  return decryptMsg
}

export { encryption, decryption }
